package com.RKB.service.impl;

import com.RKB.dao.questionbankDao;
import com.RKB.pojo.*;
import com.RKB.service.questionbankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class questionbankServiceimpl implements questionbankService {
@Autowired
    private questionbankDao questionbankdao;
    @Override
    public List<questionbank> findAll() {
        return questionbankdao.findAll();
    }

    @Override
    public List<questionbank> findByQuestion(String question) {
        return questionbankdao.findByQuestion(question);
    }

    @Override
    public void toAddQuestion(questionbank addquestion) {
        questionbankdao.toAddQuestion(addquestion);
    }

    @Override
    public List<questionbank> findByType(String type) {
        return questionbankdao.findByType(type);
    }

    @Override
    public List<Integer> findIdByType(String type) {
        return questionbankdao.findIdByType(type);
    }

    @Override
    public List<questionbank> findById(Integer id) {
        return questionbankdao.findById(id);
    }

    @Override
    public List<answerType> findCorrect() {
        return questionbankdao.findCorrect();
    }

    @Override
    public String findTypeById(Integer id) {
        return questionbankdao.findTypeById(id);
    }

    @Override
    public void insertErr_log(Err_log errLog) {
        questionbankdao.insertErr_log(errLog);
    }

    @Override
    public void insertErr_Questions(List<Err_questionbank> errQuestionbanks) {
        questionbankdao.insertErr_question(errQuestionbanks);
    }

//    获取该用户错题号以及对应日期
    @Override
    public List<Err_questionIds_time> getErr_ids(String username) {
        List<Err_questionIds_time> errIds = questionbankdao.getErr_ids(username);
        return errIds;
    }

//    获取该用户的所有具体错题内容以及日期
    @Override
    public List<Err_questions_time> getErr_questionbank(String username) {
        List<Err_questionIds_time> errIds = this.getErr_ids(username);
        List<Err_questions_time> result = new ArrayList<>();
        for (Err_questionIds_time err: errIds
             ) {
            Err_questions_time errQuestionsTime = new Err_questions_time();
            List<Integer> ids = new ArrayList<>();
            List<questionbank> questionbanks = new ArrayList<>();
            String errorId = err.getError_id();
            String[] split = errorId.split(",");
            for (int i = 0; i < split.length; i++) {
                ids.add(Integer.parseInt(split[i]));
            }
            for (Integer id: ids
                 ) {
                questionbank questionbank = this.findById(id).get(0);
                questionbanks.add(questionbank);
            }
            System.out.println("时间:"+err.getTime());
            errQuestionsTime.setTime(err.getTime());
            errQuestionsTime.setQuestionbank(questionbanks);
            result.add(errQuestionsTime);
        }
        return result;
    }
}
